/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.cookor.poi.modules.weixin.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cookor.poi.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.modules.weixin.entity.TWeixinConfig;
import com.cookor.poi.modules.weixin.service.TWeixinConfigService;

/**
 * 微信tokenController
 * @author poi
 * @version 2021-03-16
 */
@Controller
@RequestMapping(value = "${adminPath}/weixin/tWeixinConfig")
public class TWeixinConfigController extends BaseController {

	@Autowired
	private TWeixinConfigService tWeixinConfigService;

	@ModelAttribute
	public TWeixinConfig get(@RequestParam(required=false) String id) {
		TWeixinConfig entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tWeixinConfigService.get(id);
		}
		if (entity == null){
			entity = new TWeixinConfig();
		}
		return entity;
	}

	@RequiresPermissions("weixin:tWeixinConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(TWeixinConfig tWeixinConfig, HttpServletRequest request, HttpServletResponse response, Model model) {
		tWeixinConfig.setTenantId(UserUtils.getTenantId());
		Page<TWeixinConfig> page = tWeixinConfigService.findPage(new Page<TWeixinConfig>(request, response), tWeixinConfig);
		model.addAttribute("page", page);
		model.addAttribute("tWeixinConfig", tWeixinConfig);
		return "modules/weixin/tWeixinConfigList";
	}

	@RequiresPermissions("weixin:tWeixinConfig:view")
	@RequestMapping(value = "form")
	public String form(TWeixinConfig tWeixinConfig, Model model) {
		model.addAttribute("tWeixinConfig", tWeixinConfig);
		model.addAttribute("admin", UserUtils.getUser());
		return "modules/weixin/tWeixinConfigForm";
	}

	@RequiresPermissions("weixin:tWeixinConfig:edit")
	@RequestMapping(value = "save")
	public String save(TWeixinConfig tWeixinConfig, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tWeixinConfig)){
			return form(tWeixinConfig, model);
		}
		tWeixinConfigService.save(tWeixinConfig);
		addMessage(redirectAttributes, "保存微信token成功");
		return "redirect:"+Global.getAdminPath()+"/weixin/tWeixinConfig/?repage";
	}

	@RequiresPermissions("weixin:tWeixinConfig:edit")
	@RequestMapping(value = "delete")
	public String delete(TWeixinConfig tWeixinConfig, RedirectAttributes redirectAttributes) {
		tWeixinConfigService.delete(tWeixinConfig);
		addMessage(redirectAttributes, "删除微信token成功");
		return "redirect:"+Global.getAdminPath()+"/weixin/tWeixinConfig/?repage";
	}

}
